<?php
/**
 * author  :  hation
 *  email  : 331596350@qq.com
 * datetime: 2018/2/2 9:45
 */
namespace App\JuheHelper;

/**
 * 调用聚合接口的助手类
 * Class JuheApi
 * @package App\Helper
 */
class JuheApi implements JuheSearch
{
    protected $url;
    /**
     * @var string 项目密钥
     */
    protected $appKey;
    protected $urlMapping=[
        'trademark_list'=>'http://v.juhe.cn/trademark/search',
        'trademark_applicant_list'=>'http://v.juhe.cn/trademark/applist',
        'applicant_trademark_list'=>'http://v.juhe.cn/trademark/marklist',
        'trademark_detail'=>'http://v.juhe.cn/trademark/detail',
        'patent'=>'http://v.juhe.cn/patent/search.php',
        'patent_info'=>'http://v.juhe.cn/patent/base.php',
        'patent_transaction'=>'http://v.juhe.cn/patent/transaction.php',
        'patent_invoke'=>'http://v.juhe.cn/patent/invoke.php',
        'patent_similar'=>'http://v.juhe.cn/patent/similar.php',
    ];
    /**
     * JuheApi constructor.
     * @param $appKey
     */
    public function __construct($appKey)
    {
        $this->appKey=$appKey;
    }

    /**
     * 查看商标列表
     * @param  array $data 需要查询的数据
     * @return array|bool false 失败
     */
    function searchTrademarkList($data)
    {
        $this->url=$this->setHost('trademark_list');
        return $this->send($data);
    }
    function searchTrademarkApplicantList($data)
    {
        // TODO: Implement searchTrademarkApplicantList() method.
    }

    function searchApplicantTrademarkList($data)
    {
        // TODO: Implement searchApplicantTrademarkList() method.
    }

    /**
     * 查看商标详情
     * @param $data
     * @return array|bool
     */
    function searchTrademarkDetail($data)
    {
        $this->url=$this->setHost('trademark_detail');
        return $this->send($data);
    }

    function searchPatent($data)
    {
        $this->url=$this->setHost('patent');
        return $this->send($data);
    }

    function searchPatentInfo($data)
    {
        $this->url=$this->setHost('patent_info');
        return $this->send($data);
    }

    function searchPatentTransaction($data)
    {
        // TODO: Implement searchPatentTransaction() method.
    }

    function searchPatentInvoke($data)
    {
        // TODO: Implement searchPatentInvoke() method.
    }

    function searchPatentSimilar($data)
    {
        // TODO: Implement searchPatentSimilar() method.
    }
    function setHost($host)
    {
        return $this->urlMapping[$host];
    }

    /**
     * 发送查询请求
     * @param $data array 路径参数
     * @return bool|array
     */
    private function send($data)
    {
        $params=$this->combineParams($data);
        $url = $this->url."?".$params;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $result = curl_exec($ch);
        curl_close($ch);
        $res= json_decode($result, true);
        switch ($res["reason"]){
            //专利的返回数据
            case "success":
                return $res['result'];
            //商标的返回数据
            case "Success":
                return $res['result']['data'];
            default: return false;
        }
    }

    /**
     * 拼接参数
     * @param $arr array 需要拼接的关联数组
     * @return string 拼接后的数组
     */
    function combineParams($arr)
    {
        $str='key='.$this->appKey;
        foreach ($arr as $k =>$v){
            $str.='&'.$k.'='.$v;
        }
        return $str;
    }
}